package com.yufubao.api.controller.applet.authorization;

import com.yufubao.framework.annotation.SystemLog;
import com.yufubao.framework.constant.AppletRedisKeysConstant;
import com.yufubao.framework.enums.CommonExceptionEnum;
import com.yufubao.framework.exception.BaseException;
import com.yufubao.framework.interceptor.AppletInterceptor;
import com.yufubao.framework.interceptor.local.LoginUserLoc;
import com.yufubao.framework.result.PageUtils;
import com.yufubao.framework.result.ResponseResult;
import com.yufubao.model.pojo.applet.authorization.UserEntity;
import com.yufubao.service.service.applet.authorization.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

import static com.yufubao.framework.util.SecurityUtils.checkUserLoc;


/**
 * 用户记录表
 *
 * @author zhangzhan
 * @email 52194440@qq.com
 * @date 2024-08-26 10:04:12
 */
@RestController
@RequestMapping("/applet/yufubaouser")
@Api("用户记录表")
public class UserController {


    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 退出登录
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    @SystemLog(info = "退出登录")
    @ApiOperation(value = "退出登录")
    public ResponseResult logout() {
        LoginUserLoc loginUserLoc = checkUserLoc(AppletInterceptor.APPLETTHREADLOCAL);
        //删除缓存
        Boolean res = stringRedisTemplate.delete(AppletRedisKeysConstant.APPLET_LOGIN + loginUserLoc.getUserId());
        if (res != null && res) {
            return ResponseResult.ok();
        } else {
            throw new BaseException(CommonExceptionEnum.TCSB_QCS);
        }
    }


    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @SystemLog(info = "用户记录表查询")
    @ApiOperation(value = "用户记录表查询")
    public ResponseResult list(@RequestBody Map<String, Object> params) {
        PageUtils page = userService.queryPage(params);

        return ResponseResult.okResultPage("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping(value = "/info/{userId}", method = RequestMethod.GET)
    @SystemLog(info = "用户记录表查询Byid")
    @ApiOperation(value = "用户记录表查询Byid")
    public ResponseResult info(@PathVariable("userId") Integer userId) {
        UserEntity yufubaoUser = userService.getById(userId);

        return ResponseResult.okResultPage("yufubaoUser", yufubaoUser);
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @SystemLog(info = "用户记录表保存")
    @ApiOperation(value = "用户记录表保存")
    public ResponseResult save(@RequestBody UserEntity yufubaoUser) {
        userService.save(yufubaoUser);

        return ResponseResult.ok();
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @SystemLog(info = "用户记录表修改")
    @ApiOperation(value = "用户记录表修改")
    public ResponseResult update(@RequestBody UserEntity yufubaoUser) {
        userService.updateById(yufubaoUser);

        return ResponseResult.ok();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @SystemLog(info = "用户记录表删除")
    @ApiOperation(value = "用户记录表删除")
    public ResponseResult delete(@RequestBody Integer[] userIds) {
        userService.removeByIds(Arrays.asList(userIds));

        return ResponseResult.ok();
    }

}
